package pl.register.android.activities.correlation.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import pl.register.android.R;
import pl.register.android.classes.Classes;
import pl.register.android.homework.Homework;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.program.Program;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-07
 */
public class CorrelationHolidayLessonAdapter {

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    private LayoutInflater inflater;

    private LinearLayout llClasses;

    private Program program;

    private List<Lesson> lessons;

    public CorrelationHolidayLessonAdapter(View rowView, LayoutInflater inflater, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.inflater = inflater;
        this.onCheckedChangeListener = onCheckedChangeListener;
        program = new Program(new ArrayList<LessonProgram>());
        lessons = new ArrayList<Lesson>();
        llClasses = (LinearLayout) rowView.findViewById(R.id.classes_linear_layout);
    }

    private void clearRow() {
        llClasses.removeAllViews();
    }

    private void build() {
        clearRow();
        for (Lesson lesson : lessons) {
            llClasses.addView(getClassesPresenceView(lesson.getClasses()));
        }
    }

    private View getClassesPresenceView(List<Classes> classes) {
        View view = null;
        if (classes.size() == 1) {
            view = inflater.inflate(R.layout.table_cell_classes_presence, null);
            Classes classes1 = classes.get(0);
            CheckBox cbClasses = (CheckBox) view.findViewById(R.id.presence_check_box);
            cbClasses.setChecked(classes1.isPresence());
            cbClasses.setTag(classes1);
            cbClasses.setOnCheckedChangeListener(onCheckedChangeListener);
        } else if (classes.size() == 2) {
            view = inflater.inflate(R.layout.table_cell_classes_double_presence, null);
            Classes classes1 = classes.get(0);
            CheckBox cbClasses1 = (CheckBox) view.findViewById(R.id.presence_1_check_box);
            cbClasses1.setChecked(classes1.isPresence());
            cbClasses1.setTag(classes1);
            cbClasses1.setOnCheckedChangeListener(onCheckedChangeListener);
            Classes classes2 = classes.get(1);
            CheckBox cbClasses2 = (CheckBox) view.findViewById(R.id.presence_2_check_box);
            cbClasses2.setChecked(classes2.isPresence());
            cbClasses2.setTag(classes2);
            cbClasses2.setOnCheckedChangeListener(onCheckedChangeListener);
        } else {
            Toast.makeText(inflater.getContext(), "Ilość zajęć nie obsługiwana.", Toast.LENGTH_SHORT).show();
        }
        return view;
    }

    public void refresh(Program program, List<Lesson> lessons) {
        this.program = program;
        this.lessons = lessons;
        build();
    }

}